#include <myhead.h>
#include "page.h"

void* page_to_myssh_index(){
	//进入首页时开启心跳，因为本章所有暂时关闭心跳
	//为了方便，统一在此开启
	client_start_heart((char*)client_loginname());
	print_nav2("首页/服务器远程控制功能", (char*)client_loginname());
	printf("[指令选项]:\n");
	printf("1-发送ssh指令\n");
	printf("2-上传文件\n");
	printf("3-下载文件\n");
	printf("P-返回上一页\n");

	char cmd=getInputChar(NULL);
	switch(cmd){
	case '1':
		return page_to_myssh;
	case '2':
		return page_to_myssh_upload;
	case '3':
		return page_to_myssh_download;
	case 'P':
		return page_to_index;
	}
	print_red("不支持的指令");
	return page_to_myssh_index;
}

void* page_to_myssh(pkg_t* retPkg, char* errmsg){
	//暂时关闭心跳
	client_stop_heart();
	print_nav2("首页/服务器远程控制功能/MYSSH", (char*)client_loginname());
	printf("[输入SSH指令]:");
	
	char cmd[1024]={0};
	fgets(cmd, sizeof(cmd), stdin);
	cmd[strlen(cmd)-1]=0;
	printf("客户端，cmd=[%s]\n", cmd);

	//打包协议报
	char sendBuf[4096]={0};
	int len=pkg_pack_buf(sendBuf, MSG_TYPE_MYSSH,1, cmd);
	//通信
	int ret=client_sendAndRet(sendBuf, len, retPkg, errmsg);              
	if(ret != 0){
		print_red2("远程SSH失败", errmsg);
		return page_to_myssh_index;
	}
	if(strcmp("OK", pkg_get(retPkg, 0)) != 0){
		print_red2("SSH指令执行失败", errmsg);
		return page_to_myssh_index;
	}

	print_green("SSH指令执行成功, 执行结果:");
	printf("%s\n", pkg_get(retPkg, 1));
	return page_to_myssh_index;
}
